package com.sword.controller.buy;

import com.sword.constant.MessageConstant;
import com.sword.page.PageResult;
import com.sword.page.QueryPageBean;
import com.sword.page.Result;
import com.sword.pojo.buy.Buyorder;
import com.sword.pojo.buy.BuyorderDetail;
import com.sword.pojo.finance.Receivables;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.sword.service.impl.buy.BuyorderServiceImpl;
import com.sword.service.impl.buy.BuyorderDetailImpl;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/buy")
public class BuyorderController {
    @Autowired
    private BuyorderServiceImpl buyorderService;
    @Autowired
    private BuyorderDetailImpl BuyorderDetailService;

    @RequestMapping("/buyorder")
    public String buyorder() {
        return "views/buy/buyorder";
    }

    @RequestMapping("/buyorderdetail")
    public String buyorderdetail() {
        return "views/buy/buyorderDetail";
    }

    /**
     查询所有退库单
     */
    @RequestMapping("/queryBuyorder")
    @ResponseBody
    public PageResult queryBuyorder(@RequestBody QueryPageBean queryPageBean) {
        try {
            return buyorderService.queryBuyorder(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(500, MessageConstant.QUERY_BUYORDER_FAIL, null, null);
        }
    }
    /**
     * 添加退库单功能
     */

    @ResponseBody
    @RequestMapping("/addBuyorder")
    public Result addBuyorder(@RequestBody Buyorder buyorder) {
        try {
            // 获取session保存进数据
          //  String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
           // buyorder.set(userId);
            // 判断是否添加成功
            Integer flag = buyorderService.addBuyorder(buyorder);
            if (flag <= 0) {
                return new Result(500, MessageConstant.ADD_BUYORDER_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.ADD_BUYORDER_FAIL, null);
        }
        return new Result(200, MessageConstant.ADD_BUYORDER_SUCCESS, null);
    }
    /**
     * 修改退库单
     */
    @ResponseBody
    @RequestMapping("editBuyorder")
    public Result editBuyorder(@RequestBody Buyorder buyorder) {
        try {
            Integer flag = buyorderService.editBuyorder(buyorder);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_BUYORDER_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_BUYORDER_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_BUYORDER_SUCCESS, null);
    }

    /**
     * 删除退库单
     */

    @ResponseBody
    @RequestMapping("deleteBuyorder")
    public Result deleteBuyorder(Integer id) {
        try {
            Integer flag = buyorderService.deleteBuyorder(id);
            if (flag <= 0) {
                return new Result(500, MessageConstant.DELETE_BUYORDER_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.DELETE_BUYORDER_FAIL, null);
        }
        return new Result(200, MessageConstant.DELETE_BUYORDER_SUCCESS, null);
    }

    /**
     查询所有退库明细
     */
    @RequestMapping("/queryBuyorderDetail")
    @ResponseBody
    public PageResult queryBuyorderDetail(@RequestBody QueryPageBean queryPageBean) {
        try {
            return BuyorderDetailService.queryBuyorderDetail(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(500, MessageConstant.QUERY_BUYORDERDETAIL_FAIL, null, null);
        }
    }
    /**
     * 添加退库明细功能
     */

    @ResponseBody
    @RequestMapping("/addBuyorderDetail")
    public Result addBuyorderDetail(@RequestBody BuyorderDetail buyorderDetail) {
        try {
            // 获取session保存进数据
            String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
            buyorderDetail.setCreateid(userId);
            buyorderDetail.setStatus(0);
            // 判断是否添加成功
            Integer flag = BuyorderDetailService.addBuyorderDetail(buyorderDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.ADD_BUYORDERDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.ADD_BUYORDERDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.ADD_BUYORDERDETAIL_SUCCESS, null);
    }
    /**
     * 修改退库明细
     */
    @ResponseBody
    @RequestMapping("editBuyorderDetail")
    public Result editBuyorderDetail(@RequestBody BuyorderDetail buyorderDetail) {
        try {
            Integer flag = BuyorderDetailService.editBuyorderDetail(buyorderDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_BUYORDERDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_BUYORDERDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_BUYORDERDETAIL_SUCCESS, null);
    }

    /**
     * 删除退库明细
     */

    @ResponseBody
    @RequestMapping("deleteBuyorderDetail")
    public Result deleteBuyorderDetail(Integer id) {
        try {
            Integer flag = BuyorderDetailService.deleteBuyorderDetail(id);
            if (flag <= 0) {
                return new Result(500, MessageConstant.DELETE_BUYORDERDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.DELETE_BUYORDERDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.DELETE_BUYORDERDETAIL_SUCCESS, null);
    }
    @ResponseBody
    @RequestMapping("examineBuyorderDetail")
    public Result examineBuyorderDetail(@RequestBody BuyorderDetail buyorderDetail) {
        try {
            Integer flag = BuyorderDetailService.examineBuyorderDetail(buyorderDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_BUYORDERDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_BUYORDERDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_BUYORDERDETAIL_SUCCESS, null);
    }
    /**
     * 弃审退货明细
     */
    @ResponseBody
    @RequestMapping("unexamineBuyorderDetail")
    public Result unexamineBuyorderDetail(@RequestBody BuyorderDetail buyorderDetail) {
        try {
            Integer flag = BuyorderDetailService.unexamineBuyorderDetail(buyorderDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_BUYORDERDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_BUYORDERDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_BUYORDERDETAIL_SUCCESS, null);
    }

}
